<?php

namespace app\admin\controller;

use app\common\model\User;

use think\facade\Db;
use think\facade\View;
//use think\facade\Request;
use think\Request;

class Dashboard extends Base
{
    public function index(Request $request)
    {
        $where = [
            ['status', '<>', config('constants.SOFT_DELETE_STATUS')],
        ];
        //$firstDay = date('Y-m-01') . ' 00:00:00';
        //$lastDay = date('Y-m-d', strtotime("$firstDay +1 month -1 day")) . ' 23:59:59';
        $firstDay = mktime(0,0,0, date('m'),1, date('Y'));
        $lastDay = mktime(23,59,59, date('m'), date('t'), date('Y'));
        // 总订单
        $totalOrder = mt_rand(0, 999);
        // 总订单金额
        $orderAmount = mt_rand(0, 999);
        // 总商品
        $totalGoods = mt_rand(0, 999);
        // 本月会员
        $monthUser = User::where($where)->whereTime('created_at', 'month')->count();
        // 总会员
        $totalUser = User::where($where)->count();

        // 订单统计
        //$orderStatistics = \app\common\service\Order::getCharts();

        $data = [
            'totalOrder' => $totalOrder,
            'orderAmount' => $orderAmount,
            'totalGoods' => $totalGoods,
            'monthUser' => $monthUser,
            'totalUser' => $totalUser,
            //'orderStatistics' => json_encode($orderStatistics),
        ];

        return View::fetch('dashboard/index', [
            'data' => $data
        ]);
    }

    /**
     * 内容统计
     */
    public function cmsCharts(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->param();
            $where = [
                ['status', '<>', config('constants.SOFT_DELETE_STATUS')]
            ];
            $statistics = \app\common\service\Article::getCharts($where, []);

            return json([
                'status' => 1,
                'data' => $statistics
            ]);
        }
    }
}